package com.alatus;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;

public class Rect extends JFrame {
    //    屏幕分辨率
    public static int screen_W = 1024;
    public static int screen_H = 682;
    public static int half_screen_W = screen_W / 2;
    public static int half_screen_H = screen_H / 2;
    //    记载当前已经绚烂的帧数
    public static int frameIndex;
    public static int screenSize = screen_W * screen_H;
    //    希望达到的每帧之间的间隔时间
    public static int frameInterval = 33;
    //    使用jPanel作为画板
    public static JPanel panel;
    //    使用一个int数组来存储屏幕像素数值
    public static int[] screen;
    //    图像缓冲区,提供了在内存中操作屏幕图像的方式
    public static BufferedImage screenBuffer;
    //    刷新率,计算刷新率所用的辅助参数
    public static int framePerSecond;
    public static long lastDraw;
    public static double lastTime,thisTime;
    //    cpu睡眠时间
    public static int sleepTime;
    public static void main(String[] args) {
        new Rect();
    }
    public Rect() {
//        弹出一个JPanel窗口, 设置窗口大小,并将它放置在屏幕中间
        setTitle("三角形矢量计算");
        panel = (JPanel) this.getContentPane();
        panel.setPreferredSize(new Dimension(screen_W, screen_H));
        panel.setMinimumSize(new Dimension(screen_W, screen_H));
        panel.setLayout(null);
        setResizable(false);
        pack();
        setVisible(true);
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(dimension.width / 2 - this.getSize().width / 2, dimension.height / 2 - screen_H / 2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        使用TypeIntRGB来创建BufferedImage,然后把屏幕的像素数组指向BufferedImage中的DataBuffer
//        这样通过改变屏幕的像素数组中的数据就可以在屏幕上渲染出图象
        screenBuffer = new BufferedImage(screen_W, screen_H, BufferedImage.TYPE_INT_RGB);
        DataBuffer dest = screenBuffer.getRaster().getDataBuffer();
        screen = ((DataBufferInt)dest).getData();
//        初始化查找表
        LookupTable.init();
//        渲染
        while(true){
//        把图像发送到显存,这是唯一用到显卡的地方
            int r_skyBlue = 163, g_skyBlue = 216, b_skyBlue = 239;
            int r_orange = 255, g_orange = 128, b_orange = 0;
//            渲染为天蓝色
            screen[0] = (163 << 16) | (216 << 8) | b_skyBlue;//天蓝色
            for (int i = 0; i < screenSize; i++) {
//                arrayCopy是JAVA中为数不多的不适用JVM虚拟机而是直接使用系统资源计算的命令和方法,所以它的效率非常高
//                这里这一步等了超级久它才渲染出来,不知道为什么???
                System.arraycopy(screen,0,screen,i,screenSize - i >= i ? i : screenSize - i);
            }
//            loop每次运行,帧数+1
            frameIndex++;
//            计算当前的刷新率,并尽量让刷新率恒定不变
            if(frameIndex%30==0){
                double thisTime = System.currentTimeMillis();
                framePerSecond = (int) (1000 / ((thisTime - lastTime)/30));
                lastTime = thisTime;
            }
            sleepTime = 0;
            while(System.currentTimeMillis() - lastDraw < frameInterval){
                try{
                    Thread.sleep(1);
                    sleepTime++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            lastDraw = System.currentTimeMillis();
//            显示当前刷新率
            Graphics2D e2 = (Graphics2D) screenBuffer.getGraphics();
            e2.setColor(Color.BLACK);
            e2.drawString("FPS:"+framePerSecond+"ThreadSleep:"+sleepTime,5,15);
            panel.getGraphics().drawImage(screenBuffer, 0, 0, this);
        }
    }
}
